/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

export class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.next = (next === undefined ? null : next)
    }
}
 // if (!head) return null;
    // // 栈
    // const stack: number[] = []
    // let temp: ListNode | null = head;

    // while (temp) {
    //     // 当这个玩意不为空时 进入循环 入栈
    //     stack.push(temp.val);
    //     temp = temp.next;
    // }
    // console.log(stack)
    // // 全部入栈之后出栈
    // const result: ListNode | null = null;
    // for (let i = stack.length; i > 0; i--) {
    //     const temp = new ListNode(stack[i]);
    // }
    // return null
function reverseList(head: ListNode | null): ListNode | null {
   
    let pre = null;
    let cur = head;
    while(cur) {
        const next = cur.next;
        cur.next  = pre;
        pre = cur;
        cur = next;
    }
    // console.log(pre);
    return pre;
};

const list: ListNode = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))))

// console.log(list)
reverseList(list);